package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 董万均
 * @since 2020-10-24
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类中的业务
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            InputStream inputStream = file.getInputStream();
            //第一个参数：输入流，第二个参数：与Excel表对应的实体类，第三个参数：监听器
            EasyExcel.read(inputStream, SubjectData.class,new SubjectListener(subjectService)).sheet().doRead();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> queryAllSubject() {
        QueryWrapper<EduSubject> oneWrapper=new QueryWrapper<>();
        oneWrapper.eq("parent_id",'0');
        //1、查询一级分类
        //查询的方法有两种
         //第一种
        //this.list(wrapper)
        //第二种：自动注入mapper的，但是继承ServiceImpl中自动注入的baseMapper
        List<EduSubject> oneSubject = baseMapper.selectList(oneWrapper);
        //2、查询二级分类
        QueryWrapper<EduSubject> twoWrapper=new QueryWrapper<>();
        twoWrapper.ne("parent_id",'0');
        List<EduSubject> twoSubject = baseMapper.selectList(twoWrapper);
        //3、封装一级分类
        //创建一个数组集合，里面有一级分类和二级分类
        List<OneSubject> oneSubjectList=new ArrayList<>();
        //遍历一级分类，将一级分类中的对象放到数组集合中
        for (int i = 0; i < oneSubject.size(); i++) {
            EduSubject subject1= oneSubject.get(i);
            //创建OneSubject对象
            OneSubject one=new OneSubject();
            //这是第一种方式，将subject的值赋给one，但是不适用于大量的数据
            //one.setId(subject.getId());
            //one.setTitle(subject.getTitle());
            //第二种方式
            BeanUtils.copyProperties(subject1,one);
            oneSubjectList.add(one);

            //4、封装二级分类,二级分类是在一级分类的里面,二级分类是一个数组
            List<TwoSubject> twoSubjectList=new ArrayList<>();
            //遍历二级分类
            for (int j = 0; j < twoSubject.size(); j++) {
                EduSubject subject2 = twoSubject.get(j);
                //判断二级分类的父id和一级分类的id是否相同
                if (subject2.getParentId().equals(subject1.getId())){
                    TwoSubject two=new TwoSubject();
                    BeanUtils.copyProperties(subject2,two);
                    twoSubjectList.add(two);
                }
            }

            //一级分类中有多个二级分类
            one.setChildren(twoSubjectList);
        }

        //5、返回一级分类
        return oneSubjectList;
    }
}
